/*
 * Register2 class wraps 2 bit registers 
 * */

package cpu.datastores;

import cpu.datatypes.Bit;

public class Register2 {
	private static final int LENGTH = 2;
	private Bit[] bits = new Bit[LENGTH];
	
	public Register2( ) {
		for( int i = 0; i < LENGTH; i++ ) {
			bits[i] = new Bit( );
		}
	}
	public Bit bit( int i ) {
		assert( i >= 0 && i < LENGTH );
		return bits[i];
	}
	public int toJavaInt( ) {
		int accumulator = 0;
		for( int i = 0; i < LENGTH; i++ ) {
			if( bit( i ).value )
				accumulator += java.lang.Math.pow( 2, i );
		}
		return accumulator;
	}
	public boolean fromJavaInt( int value ) {
		// note return value is overflow
		int accumulator = value;
		for( int i = LENGTH - 1; i >= 0; i-- ) {
			if( accumulator / java.lang.Math.pow( 2, i ) >= 1 ) {
				bit( i ).value = true;
				accumulator -= java.lang.Math.pow( 2, i );
			} else {
				bit( i ).value = false;
			}
		}
		if( accumulator != 0 ) 
			return true;		// overflow
		return false;			// no overflow
	}
}
